package com.gyc.game.cchess;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * 处理 电脑计算动画提示 长将 3-3?
 * 
 * @author guanyc
 * 
 */
public class MainActivity extends Activity {

	private static final String BUNDLE_ARRAY = "bunlde_array";

	private static final String BUNDLE_CALCULATING = "bundle_calculating";

	private static final String BUNDLE_FINISHED = "bundle_game_finished";

	private static final int MENU_DIFF_AMA = 11;
	private static final int MENU_DIFF_NORMAL = 12;
	private static final int MENU_DIFF_PRO = 13;
	private static final int MENU_ID_REGRET = 10;

	private static final int MENU_ID_LOCAL = 1;
	private static final int MENU_DIFF = 2;
	private static final int MENU_ID_STOP_PC = 3;
	private static final int MENU_ID_RANGZI = 4;

	private static final int MENU_RZ_NO = 20;
	private static final int MENU_RZ_MA_1 = 21;
	private static final int MENU_RZ_MA_2 = 22;
	private static final int MENU_RZ_PAO_1 = 23;
	private static final int MENU_RZ_PAO_2 = 24;

	GameView view;

	public TextView tvHuman;

	private MenuItem diff1;
	private MenuItem diff2;
	private MenuItem diff3;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		System.out.println("oncrate 000000000000000");

		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.main);

		this.getWindow().setType(Window.FEATURE_NO_TITLE);

		// Display display = this.getWindowManager().getDefaultDisplay();

		// Log.i("display", "" + display.getWidth() + "-" +
		// display.getHeight());

		this.view = (GameView) this.findViewById(R.id.gameview);

		this.view.loadTile(1, this.getResources().getDrawable(
				R.drawable.chess_units));

		this.tvHuman = (TextView) this.findViewById(R.id.txt_human);

		array = "";

		this.view.startNewGame(1);

		if (savedInstanceState != null) {

		} else {

			System.out.println(" 44444444444444444444");

			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

			if (settings.getInt(BUNDLE_FINISHED, 0) > 0) {

			} else if (settings.getInt(BUNDLE_FINISHED, 0) < 0) {

				boolean cal = settings.getBoolean(BUNDLE_CALCULATING, false);

				array = settings.getString(BUNDLE_ARRAY, "");
				System.out.println(array);
				this.view.cbp.bp_all.decode(array);
				// view.cbp.bp_all.setXY((byte) 82, (byte) 0);
				// view.cbp.bp_all.setXY((byte) 88, (byte) 0);
				view.repaint();

			}

			SharedPreferences.Editor editor = settings.edit();
			editor.clear();
			editor.commit();
		}

		/*
		 * Button but = new Button(this); but.setText("" + chars.get(i)); int fp
		 * = LinearLayout.LayoutParams.FILL_PARENT; int wc =
		 * LinearLayout.LayoutParams.WRAP_CONTENT;
		 * 
		 * linearl.addView(but, new LinearLayout.LayoutParams(fp, wc, 1));
		 */

		/*
		 * LinearLayout ll = new LinearLayout(this);
		 * 
		 * int fp = LinearLayout.LayoutParams.FILL_PARENT; int wc =
		 * LinearLayout.LayoutParams.WRAP_CONTENT; ll.setLayoutParams(new
		 * LinearLayout.LayoutParams(fp, wc, 1));
		 * ll.setBackgroundColor(Color.BLUE); this.setContentView(ll);
		 * 
		 * System.out.println("ll.getMeasuredHeight()" +
		 * ll.getMeasuredHeight());//0
		 * System.out.println("ll.getMeasuredWidth()" +
		 * ll.getMeasuredWidth());//0
		 */

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_ID_LOCAL, 0, "开始");
		menu.add(0, MENU_ID_REGRET, 1, "悔 棋");
		// menu.add(0, MENU_ID_STOP_PC, 2, "停止");

		SubMenu x = menu.addSubMenu(0, MENU_DIFF, 3, "难度");
		diff1 = x.add(1, MENU_DIFF_AMA, 1, " 简 单 ");
		diff2 = x.add(1, MENU_DIFF_NORMAL, 2, " 一 般 ");
		// diff3 = x.add(1, MENU_DIFF_PRO, 2, "困难");
		diff1.setCheckable(true);
		diff2.setCheckable(true);
		// diff3.setCheckable(true);

		diff1.setChecked(true);

		SubMenu rz = menu.addSubMenu(0, MENU_ID_RANGZI, 4, "让子局");
		// { "无", "1马", "2马", "1炮", "2炮" };
		rz.add(1, MENU_RZ_NO, 0, " 无 ");
		rz.add(1, MENU_RZ_MA_1, 1, " 一马 ");
		rz.add(1, MENU_RZ_MA_2, 2, " 二马 ");
		rz.add(1, MENU_RZ_PAO_1, 3, " 一炮 ");
		rz.add(1, MENU_RZ_PAO_2, 4, " 二炮 ");

		return true;
	}

	int depth;

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		if (id == MENU_RZ_NO) {
			if (view.calculating) {
				return true;
			}
		} else if (id == MENU_RZ_MA_1) {
			if (view.calculating) {
				return true;
			}

			if (this.depth != 0) {
				view.startNewGame(depth);
			} else {
				view.startNewGame(1);
			}
			view.cbp.bp_all.setXY((byte) 82, (byte) 0);
			view.repaint();
		} else if (id == MENU_RZ_MA_2) {
			if (view.calculating) {
				return true;
			}

			if (this.depth != 0) {
				view.startNewGame(depth);
			} else {
				view.startNewGame(1);
			}

			view.cbp.bp_all.setXY((byte) 82, (byte) 0);
			view.cbp.bp_all.setXY((byte) 88, (byte) 0);
			view.repaint();

		} else if (id == MENU_RZ_PAO_1) {
			if (view.calculating) {
				return true;
			}
			if (this.depth != 0) {
				view.startNewGame(depth);
			} else {
				view.startNewGame(1);
			}
			view.cbp.bp_all.setXY((byte) 64, (byte) 0);
			view.repaint();
		} else if (id == MENU_RZ_PAO_2) {
			if (view.calculating) {
				return true;
			}

			if (this.depth != 0) {
				view.startNewGame(depth);
			} else {
				view.startNewGame(1);
			}
			view.cbp.bp_all.setXY((byte) 64, (byte) 0);
			view.cbp.bp_all.setXY((byte) 70, (byte) 0);

			view.repaint();

		} else if (id == this.MENU_ID_STOP_PC) {
			// view.stopPc();
		} else if (id == this.MENU_ID_LOCAL) {
			if (this.depth != 0) {
				view.startNewGame(depth);
			} else {
				view.startNewGame(1);
			}
			return true;
		} else if (id == MainActivity.MENU_ID_REGRET) {
			view.regret();
			return true;

		} else if (id == MainActivity.MENU_DIFF_AMA) {
			this.depth = 1;

			diff1.setChecked(true);
			diff2.setChecked(false);
			return true;

		} else if (id == MainActivity.MENU_DIFF_NORMAL) {
			this.depth = 3;
			diff1.setChecked(false);
			diff2.setChecked(true);
			// diff3.setChecked(false);

			return true;

		} else if (id == MainActivity.MENU_DIFF_PRO) {
			this.depth = 5;

			diff1.setChecked(false);
			diff2.setChecked(false);
			// diff3.setChecked(true);
			return true;

		}

		return true;
	}

	@Override
	protected void onSaveInstanceState(Bundle outbundle) {
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.clear();

		if (this.view.m_bGameOver) {
			editor.putInt(BUNDLE_FINISHED, 1);
		} else {
			editor.putInt(BUNDLE_FINISHED, -1);

			if (this.view.calculating) {
				editor.putBoolean(BUNDLE_CALCULATING, this.view.calculating);
				String encode = this.view.cbp.bp_all.encode();
				System.out.println("encode:" + encode);
				editor.putString(BUNDLE_ARRAY, encode);
			} else {
				editor.putBoolean(BUNDLE_CALCULATING, this.view.calculating);
				String encode = this.view.cbp.bp_all.encode();
				System.out.println("encode:" + encode);
				editor.putString(BUNDLE_ARRAY, encode);

			}

			editor.commit();
		}

		this.view.onDestroy();

		super.onSaveInstanceState(outbundle);

	}

	public static final String PREFS_NAME = "com.gyc.game.cchess";
	private String array;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		System.out.println("ondestroy 111111111111");

		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.clear();

		if (this.view.m_bGameOver) {
			editor.putInt(BUNDLE_FINISHED, 1);
		} else {
			editor.putInt(BUNDLE_FINISHED, -1);

			if (this.view.calculating) {
				editor.putBoolean(BUNDLE_CALCULATING, this.view.calculating);
				String encode = this.view.cbp.bp_all.encode();
				System.out.println("encode:" + encode);
				editor.putString(BUNDLE_ARRAY, encode);
			} else {
				editor.putBoolean(BUNDLE_CALCULATING, this.view.calculating);
				String encode = this.view.cbp.bp_all.encode();
				System.out.println("encode:" + encode);
				editor.putString(BUNDLE_ARRAY, encode);

			}

			editor.commit();
		}

		this.view.onDestroy();

		super.onDestroy();
	}

	@Override
	public void onLowMemory() {
		Toast.makeText(this, "内存不足", Toast.LENGTH_SHORT);
		// super.onLowMemory();
		this.finish();
	}

	@Override
	protected void onPause() {
		System.out.println("onPause");
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		System.out.println("onRestart");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		System.out.println("onResume");
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		System.out.println("onStart");
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		System.out.println("onStop");
		super.onStop();
	}

}